﻿using DFGK.IService;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.PC.基础数据
{
    /// <summary>
    /// 字典类型
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class DictTypeController : BaseController
    {
        private readonly IDictTypeService dictTypeService;
        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="dictTypeService"></param>
        public DictTypeController(IWebHostEnvironment environment, IDictTypeService dictTypeService) : base(environment)
        {
            this.dictTypeService = dictTypeService;
        }

        /// <summary>
        /// 添加字典类型
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddDictType(DictType dic)
        {
            dic.CreateUserId = base.BaseGetNowUserId();
            dic.CreateUserName = base.BaseGetNowUserName();
            var result = dictTypeService.AddDictType(dic);
            if (result)
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！编码已存在！", "500"));
        }

        /// <summary>
        /// 查询字典类型
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllDictTypeList(string? keyword, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            int totalCount;
            var list = dictTypeService.GetAllDictTypeList(keyword, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetDictTypeByID(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = dictTypeService.GetDictTypeByID(id);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        }

        /// <summary>
        /// 是否查询树状字典类型
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="isTree"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllDictTypeTree(string? keyword, int isTree = 0)
        {
            bool isTreeBool = true;
            if (isTree == 0)
                isTreeBool = false;
            else
                isTreeBool = true;
            var list = dictTypeService.GetAllDictTypeTree(keyword, isTreeBool);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dictType"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdDicType(DictType dictType)
        {
            if (dictType.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(dictType.TypeCode))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
            if (dictType.TypeName == "产品状态")
            {
                return Ok(BaseResult.Result("400", "产品状态为系统状态不可修改"));
            }
            if (dictType.TypeName == "收货单据类型")
            {
                return Ok(BaseResult.Result("400", "收货单据类型为系统状态不可修改"));
            }
            dictType.ModifyUserId = base.BaseGetNowUserId();
            dictType.ModifyUserName = base.BaseGetNowUserName();
            var result = dictTypeService.UpdDicType(dictType);
            if (result)
            {
                return Ok(BaseResult.Result(result, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "修改失败！编码已存在！", "500"));
        }

        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="id">字典id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelDicType(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var IsOK = dictTypeService.DelDicType(id);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
    }
}
